﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EmpiricalBall : MonoBehaviour
{
    /// <summary> 经验球移动速度 </summary>
    float moveSpeed = 8;

    [SerializeField] GameObject effect;

    /// <summary> 检测经验球是否进入吸附范围 </summary>
    /// <param name="col">被碰撞体</param>
    void OnTriggerStay(Collider col)
    {

        gameObject.transform.LookAt(col.gameObject.transform.position);//转向玩家

        gameObject.transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed, Space.Self);//向前位移
    }

    /// <summary> 检测经验球是否被player碰到 </summary>
    /// <param name="col">被碰撞体</param>
    void OnTriggerEnter(Collider col)
    {
        if (col.gameObject.tag == "Enemy")
        {
            col.GetComponentInParent<Enemy>().SendMessage("getEmpirical");//调用player里的getEmpirical脚本
            Destroy(gameObject);
        }
        else if (col.gameObject.tag == "Player")
        {
            col.GetComponentInParent<Player>().SendMessage("getEmpirical");//调用player里的getEmpirical脚本

            GameObject eff = Instantiate(effect,col.gameObject.transform.position, col.gameObject.transform.rotation);
            eff.transform.SetParent(col.gameObject.transform.Find("model"));
            Destroy(gameObject);
        }
    }
}
